import axios from "./request.js"
import apiList from './api.js'

// 组装接口路径
// "common.init"
function getApiPath(url) {
	let apiArray = url.split(".");
	let api = apiList;
	apiArray.forEach(v => {
		api = api[v];
	});
	return api;
}

const instance = new axios();

export default function http({
  url,
  data = {},
  toastBefore = '', // 请求前加载提示
  toastAfter = true, // 请求后错误提示
}){
  let api = getApiPath(url);
  instance.interceptor.request((config, cancle)=> {
    let token = uni.getStorageSync("token")
    if (api.auth && !token) {
      store.dispatch('showAuthModal');
      uni.hideLoading()
      throw (`暂未登录,已阻止此次API请求: '${api.url}'`);
    }
    token && instance.setConfig((config)=> {
      config.header.token = token
    })
    // 如果在提示之前显示为空
    if (toastBefore !== '') {
    	uni.showLoading({
    		title: toastBefore,
    		mask: true
    	});
    }
    
    return config
    
  })
  
   /* 请求之后拦截器 */
  instance.interceptor.response((response)=> {
    uni.hideLoading();
    if (response.code === 0) {
    	if (toastAfter) {
    		uni.showToast({
    			title: response.msg || '请求出错,稍后重试',
    			icon: 'none',
    			duration: 1000,
    			mask: true
    		});
    	}
    }
    // token过期注销
    if (response.code === 401) {
    	store.dispatch('logout');
    	store.dispatch('showAuthModal');
    	throw (`登录已过期或注销,已阻止此次API请求: '${api.url}'`);
    }
    return response
  })
  
  return instance.request({
  	url: api.url,
  	data,
  	method: api.method
  })
}

